<template>
  <div class="login">
    <div class="login-item">
      <div class="left">
        <img
          :src="bannerimgs"
          alt=""
        >
      </div>
      <div class="right">
        <el-card
          shadow="always"
          :body-style="{ padding: '20px' }"
        >
          <div
            slot="header"
            style="  text-align: center;"
          >
            <img
              :src="loginimg.loginLogo"
              alt=""
            >
          </div>
          <!-- card body -->
          <el-form
            :model="form"
            ref="form"
            :rules="rules"
            label-width="80px"
            :inline="false"
            size="normal"
          >
            <el-form-item label="">
              <el-input v-model="form.account"></el-input>
              <template slot="label">
                <i class="el-icon-user"></i>

              </template>
            </el-form-item>

            <el-form-item label="">
              <el-input
                type="password"
                v-model="form.pwd"
              ></el-input>
              <template slot="label">
                <i class="el-icon-lock"></i>

              </template>
            </el-form-item>

            <el-form-item>
              <el-input
                v-model="form.code"
                class="yzm"
              ></el-input>
              <img
                class="yzm-img"
                :src="yzmimg.code"
                alt=""
                @click="getyzm()"
              >

            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="onSubmit('from')"
              >登录</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>

        </el-card>
      </div>
    </div>

  </div>
</template>
<script>
import { Message } from "element-ui";
export default {
  data() {
    return {
      form: {
        account: "demo",
        pwd: "crmeb.com",
        key: "",
        code: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        pwd: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      // 登录页面图片
      loginimg: [],
      yzmimg: [],
      bannerimgs:null
    };
  },
  methods: {
    async onSubmit(formName) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(this.$store);
          this.$store.dispatch("userdata/login", this.form).then((res) => {
            console.log(res);
            if (res) {
              this.$message({
                message: "'登录成功'",
                type: "success",
              });
              this.$router.push("/dashboard");
            } else {
              this.$message.error({
                message: "登录失败!输入有误请重新输入",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 获取登录页面图片
    getloginimg() {
      // console.log(this.$store.dispatch("userdata"));
      this.$store.dispatch("userdata/getimg").then((res) => {
        this.loginimg = res;
        // console.log(this.loginimg);
        this.bannerimgs=this.loginimg.banner[0].pic
      });
    },
    // 获取验证码
    getyzm() {
      this.$store.dispatch("userdata/yzm").then((res) => {
        // console.log(res);
        this.form.key = res.key;
        this.yzmimg = res;
      });
    },
  },
  created() {
    this.getloginimg();
    this.getyzm();
  },
  watch: {
    code(newval, oldval) {
      if (newval) {
        console.log(newval);
      }
    },
  },
};
</script>
<style lang="scss">
.login {
  background-image: url("https://image.java.crmeb.net/crmebimage/public/operation/2021/11/30/dc0c960874b6408781d119e4c84768c03n91s62hyd.jpg");
  background-size: 100% 100%;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-user {
    width: 25rem;
    margin: 200px auto;
  }
  .login-item {
    display: flex;
    .right {
      width: 25rem;
      height: 420px;

      // margin: 200px auto;
      .el-card {
        height: 100%;
        vertical-align: top;
        .el-card__body {
          .el-form {
            .yzm {
              width: 40%;
            }
            .yzm-img {
              float: right;
              margin-right: 30%;
            }
          }
        }
      }
    }
  }
}
</style>